
package com.csc440.team2.database.entities;

import java.io.Serializable;
import java.util.List;


import android.content.Context;


import com.csc440.team2.database.DBAdapter;


/**
 * Message implementation for a SMIL Message
 * 
 * @author Jeremy Spitzig
 */
public class Message implements Serializable
{
    private static final long serialVersionUID = 1L;
    private long id;
    private String message;
    private FolderType folder;
    private boolean read;
    private String sender;


    public Message()
    {
        setId(-1);
        setMessage("");
        setFolder(FolderType.SENT);
        setRead(false);
        setSender("");
    }


    public Message(long id)
    {
        setId(id);
    }


    public Message(String message, FolderType folder, boolean read, String sender)
    {
        setId(-1);
        setMessage(message);
        setFolder(folder);
        setRead(read);
        setSender(sender);
    }


    public Message(long id, String message, FolderType folder, boolean read, String sender)
    {
        setId(id);
        setMessage(message);
        setFolder(folder);
        setRead(read);
        setSender(sender);
    }


    /**
     * @return the id
     */
    public long getId()
    {
        return id;
    }


    /**
     * @param id the id to set
     */
    public void setId(long id)
    {
        this.id = id;
    }


    /**
     * @return the message
     */
    public String getMessage()
    {
        return message;
    }


    /**
     * @param message the message to set
     */
    public void setMessage(String message)
    {
        this.message = message;
    }


    /**
     * @return the folder
     */
    public FolderType getFolder()
    {
        return folder;
    }


    /**
     * @param folder the folder to set
     */
    public void setFolder(FolderType folder)
    {
        this.folder = folder;
    }


    /**
     * @return the read
     */
    public boolean isRead()
    {
        return read;
    }


    /**
     * @param read the read to set
     */
    public void setRead(boolean read)
    {
        this.read = read;
    }


    /**
     * @return the sender
     */
    public String getSender()
    {
        return sender;
    }


    /**
     * @param sender the sender to set
     */
    public void setSender(String sender)
    {
        this.sender = sender;
    }


    /**
     * Gets a reference to the database adapter
     * 
     * @param context activity context
     * @return database adapter
     */
    private static DBAdapter getDatabase(Context context)
    {
        DBAdapter adapter = new DBAdapter(context);
        adapter.open();
        return adapter;
    }


    /**
     * Loads this message from the datastore
     * 
     * @param context activity context
     * @return true if successful
     */
    public boolean load(Context context)
    {
        boolean result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.getMessage(this);
        adapter.close();
        return result;
    }


    /**
     * Save this message to the datastore
     * 
     * @param context activity context
     * @return true if successful
     */
    public boolean save(Context context)
    {
        boolean result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.saveMessage(this);
        adapter.close();
        return result;
    }


    /**
     * Deletes this message from the datastore
     * 
     * @param context activity context
     * @return true if successful
     */
    public boolean delete(Context context)
    {
        boolean result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.deleteMessage(this);
        adapter.close();
        return result;
    }


    /**
     * Gets a list of all messages in the datastore
     * 
     * @param context activity context
     * @return list of messages
     */
    public static List<Message> getAll(Context context)
    {
        List<Message> result;
        DBAdapter adapter = getDatabase(context);
        result = adapter.getAllMessages();
        adapter.close();
        return result;
    }
}
